package com.lwww.infinitechat.authenticationservice.controller;

import com.lwww.infinitechat.authenticationservice.common.Result;
import com.lwww.infinitechat.authenticationservice.data.user.login.LoginRequest;
import com.lwww.infinitechat.authenticationservice.data.user.login.LoginResponse;
import com.lwww.infinitechat.authenticationservice.data.user.register.RegisterRequest;
import com.lwww.infinitechat.authenticationservice.data.user.register.RegisterResponse;
import com.lwww.infinitechat.authenticationservice.data.common.upload.UploadUrlRequest;
import com.lwww.infinitechat.authenticationservice.data.common.upload.UploadUrlResponse;
import com.lwww.infinitechat.authenticationservice.data.user.updateAvatar.UpdateAvatarRequest;
import com.lwww.infinitechat.authenticationservice.data.user.updateAvatar.UpdateAvatarResponse;
import com.lwww.infinitechat.authenticationservice.service.UserService;
import com.lwww.infinitechat.authenticationservice.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/v1/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param request
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody RegisterRequest request){
        log.info("用户注册:{}",request);
        RegisterResponse response=userService.register(request);
        return Result.success(response);
    }

    /**
     * 用户密码登录
     * @param request
     * @return
     */
    @PostMapping("/login")
    public Result<LoginResponse> loginByPassword(@RequestBody LoginRequest request){
        log.info("用户密码登录:{}",request);
        LoginResponse response=userService.loginByPassword(request);
        return Result.success(response);
    }

    /**
     * 用户验证码登录
     * @param request
     * @return
     */
    @PostMapping("loginCode")
    public Result<LoginResponse> loginByCode(@RequestBody LoginRequest request){
        log.info("用户验证码登录:{}",request);
        LoginResponse response=userService.loginByCode(request);
        return Result.success(response);
    }

    /**
     * 用户退出
     * @param userId
     * @return
     */
    @PostMapping("/logout")
    public Result logout(String userId){
        log.info("用户退出:{}",userId);
        userService.logout(userId);
        return Result.success("退出成功");
    }

    /**
     * 用户获取OSS预授权上传链接
     * @param request
     * @return
     */
    @GetMapping("/uploadUrl")
    public Result<UploadUrlResponse> uploadUrl(UploadUrlRequest request) throws Exception {
        log.info("用户上传:{}",request);
        UploadUrlResponse response=userService.uploadUrl(request);
        return Result.success(response);
    }

    /**
     * 更新用户头像
     * @param
     * @param
     */
    @PatchMapping("/avatar")
    public Result<UpdateAvatarResponse> updateAvatar(@Valid @RequestBody UpdateAvatarRequest request, @RequestHeader String token){
        log.info("更新用户头像:{}",request,token);
        Claims claims = JwtUtil.parse(token);
        String userID = claims.getSubject();
        UpdateAvatarResponse response=userService.updateAvatar(request,userID);
        return  Result.success(response);
    }
}
